package com.hm.ibsm.window;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.apache.http.HttpResponse;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import com.alibaba.fastjson.TypeReference;
import com.hm.common.network.httpclient.HttpClientFactory;
import com.hm.common.network.httpclient.HttpClientResponseParse;
import com.hm.common.su.bean.ServerResponse;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.StringUtil;
import com.hm.ibsm.manage.vo.auth.AuthUserVo;
import com.hm.ibsm.su.GlobalContent;
import com.hm.ibsm.su.IbsmServerResponseParse;
import com.hm.ibsm.su.LocationConfig;
import com.hm.ibsm.su.R;

/**
 * @author shishun.wang
 * @date 2017年2月18日 下午4:02:23
 * @version 1.0
 * @describe
 */
public class LoginWindow extends JFrame {

	private static final long serialVersionUID = -8559759993031076353L;

	private static String LOGIN_URI = "/rest/ibsm/mg/v1.0/auth?account={0}&pwd={1}";

	private JTextField account;
	private JPasswordField pwd;
	private JLabel errorHint = new JLabel("");
	private LoginWindow self = this;

	public LoginWindow() {
		// setLocation(0, -29);
		try {
			BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
			org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();

			GlobalContent.getInstance().loadSystemLogo(this);
		} catch (Exception e3) {
			e3.printStackTrace();
		}

		this.setTitle(R.Configuration.SYSTEM_TITLE);
		this.setSize(400, 320);
		this.setLocationRelativeTo(null);
		this.getContentPane().setLayout(null);

		JLabel accountLabel = new JLabel("账号:");
		accountLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		accountLabel.setBounds(46, 95, 54, 15);
		getContentPane().add(accountLabel);

		JLabel pwdLabel = new JLabel("密码:");
		pwdLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		pwdLabel.setBounds(46, 146, 54, 15);
		getContentPane().add(pwdLabel);

		account = new JTextField();
		account.addKeyListener(loginListener);
		account.setBounds(110, 92, 232, 28);
		getContentPane().add(account);
		account.setColumns(10);

		pwd = new JPasswordField();
		pwd.setBounds(110, 140, 232, 28);
		pwd.addKeyListener(loginListener);
		getContentPane().add(pwd);
		pwd.setColumns(10);

		JButton loginBtn = new JButton("登 陆");
		loginBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loginAccount();
			}
		});
		loginBtn.addKeyListener(loginListener);
		loginBtn.setBounds(249, 210, 93, 28);
		getContentPane().add(loginBtn);
		errorHint.setFont(new Font("微软雅黑", Font.PLAIN, 11));

		errorHint.setForeground(Color.RED);
		errorHint.setBounds(113, 248, 197, 26);
		getContentPane().add(errorHint);

		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setAlwaysOnTop(true);
	}

	private void loginAccount() {
		if (StringUtil.isBlank(account.getText())) {
			errorHint.setText("账户不能为空");
			return;
		}
		if (StringUtil.isBlank(new String(pwd.getPassword()))) {
			errorHint.setText("密码不能为空");
			return;
		}
		try {
			HttpResponse response = HttpClientFactory.POST.build(LocationConfig.baseUri(LOGIN_URI), account.getText(), new String(pwd.getPassword()))
					.execute();
			AuthUserVo authUser = IbsmServerResponseParse
					.parse(HttpClientResponseParse.parseGeneric(response, new TypeReference<ServerResponse<AuthUserVo>>() {
					}));
			if (CommonUtil.isNotEmpty(authUser)) {
				GlobalContent.getInstance().setCurrentAuthUser(authUser);
				new MainWindow();
				self.setVisible(false);
			}
		} catch (Exception e1) {
			e1.printStackTrace();
			errorHint.setText(e1.getMessage());
		}
	}

	private KeyListener loginListener = new KeyListener() {

		public void keyTyped(KeyEvent e) {

		}

		public void keyReleased(KeyEvent e) {

		}

		public void keyPressed(KeyEvent e) {
			if (KeyEvent.VK_ENTER == e.getKeyCode()) {
				loginAccount();
			}
		}
	};
}
